package com.etime.shyauth.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shyauth.dao.SysRoleMapper;
import com.etime.shyauth.pojo.SysRole;
import com.etime.shyauth.services.interfaces.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色管理业务逻辑层实现类
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper mapper;
    /**
     * 插入角色
     *
     * @param role 不需要roleid
     * @return
     */
    @Override
    public int insertSysRole(SysRole role) {
        return mapper.insert(role);
    }

    /**
     * 根据id修改角色
     *
     * @param role
     * @return
     */
    @Override
    public int updateSysRoleById(SysRole role) {
        return mapper.updateById(role);
    }

    /**
     * 根据id查询角色详细信息
     *
     * @param roleid 角色id
     * @return
     */
    @Override
    public SysRole selectRoleById(int roleid) {
        return mapper.selectById(roleid);
    }

    /**
     * 根据条件查询角色列表
     *
     * @param role rolename,rolestate
     * @return
     */
    @Override
    public List<SysRole> selectRoleListByCondition(SysRole role) {
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(role!=null){
            if(role.getRolestate()!=null && role.getRolestate()!=-1){
                wrapper.eq("rolestate",role.getRolestate());
            }
            if(role.getRolename()!=null && !role.getRolename().equals("")){
                wrapper.like("rolename","%"+role.getRolename()+"%");
            }
        }
        return mapper.selectList(wrapper);
    }

    /**
     * 带分页得查询
     *
     * @param role
     * @param pagenum  第几页
     * @param pagesize 每页多少行
     * @return
     */
    @Override
    public Page<SysRole> selectRoleByPage(SysRole role, long pagenum, long pagesize) {
        Page<SysRole> page = new Page<>(pagenum,pagesize);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(role!=null){
            if(role.getRolestate()!=null && role.getRolestate()!=-1){
                wrapper.eq("rolestate",role.getRolestate());
            }
            if(role.getRolename()!=null && !role.getRolename().equals("")){
                wrapper.like("rolename","%"+role.getRolename()+"%");
            }
        }
        return mapper.selectPage(page,wrapper);
    }
}
